﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using SdmxMl.Common;

namespace SdmxMl.Structure
{
    /// <summary>
    /// Categorisation allows for the association of an identifiable object to a category,
    /// providing for the classifications of the reference identifiable object. This must
    /// either contain the full details of the categorisation, or provide a name and identification
    /// information and reference the full details from an external structure document or registry service.
    /// </summary>
    public class CategorisationType : Artefact
    {
        #region Properties
        public CategorisationSourceTarget CatSourceTarget { get; private set; }

        #endregion //Properties

        #region Constructors

        public CategorisationType()
            : base()
        {
            Init();
        }

        private void Init()
        {
            CatSourceTarget = new CategorisationSourceTarget();
        }

        public CategorisationType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr) 
            : base(nodeArtefact, nsmgr)
        {
            CatSourceTarget = new CategorisationSourceTarget(nodeArtefact, nsmgr);
        }

        #endregion //Constructors

        #region Override

        public override void WriteXmlInsider(XmlTextWriter xw)
        {
        }

        #endregion //Override

        #region Tree

        public override TreeNode FillNode(TreeNode parentNode)
        {
            TreeNode t = base.FillNode(parentNode);
            TreeNode ndSourceTarget = new TreeNode(CatSourceTarget.ToString());
            ndSourceTarget.Tag = CatSourceTarget;
            t.Nodes.Add(ndSourceTarget);
            return t;
        }
        #endregion //Tree

 
        #region Artefact Type

        public override Manager.SdmxArtefactType ArType
        {
            get { return Manager.SdmxArtefactType.Categorisations; }
        }

        #endregion    
    }

    public class CategorisationSourceTarget
    {
        // A source object is referenced via an object reference and the target category
        // is referenced via the target category.
        private ObjectRefType source;

        /// <summary>
        /// Target is reference to the category that the referenced object is to be mapped to.
        /// </summary>
        private CategoryRefType target;


        public string SourceUrn
        {
            get { return source.SourceUrn; }
            set { source.SourceUrn = value; }
        }

        public string TargetUrn
        {
            get { return target.TargetUrn; }
            set { target.TargetUrn = value; }
        }

        private void Init()
        {
            source = new ObjectRefType();
            target = new CategoryRefType();
        }

        public CategorisationSourceTarget()
        {
            Init();
        }

        public CategorisationSourceTarget(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            Init();
            XmlNode ndSource = nodeArtefact.SelectSingleNode("./structure:Source", nsmgr);
            if (ndSource != null)
                source = new ObjectRefType(ndSource, nsmgr);

            XmlNode ndTarget = nodeArtefact.SelectSingleNode("./structure:Target", nsmgr);
            if (ndTarget != null)
                target = new CategoryRefType(ndTarget, nsmgr);
        }

        public override string ToString()
        {
            return "ST";
        }
    }
}
